<html>
   <!-- $Id: recommendedUse.html,v 1.2 2006/07/13 08:54:39 grlea Exp $ -->
   <head>
      <title>Simple Log - Recommended Use</title>
      <meta http-equiv="description" content="Recommended ways of working with Simple Log" />
      <link type="text/css" rel="stylesheet" href="style.css" />
      <link type="text/css" rel="stylesheet" href="code.css" />
   </head>

   <body>

      <div class="topMenu">
         <h1>Simple Log User Guide</h1>
         <ul>
            <li><a href="quickStart.html">Quick&nbsp;Start</a></li>
            <li><a href="philosophy.html">Philosophy</a></li>
            <li><a href="recommendedUse.html">Recommended&nbsp;Use</a></li>
            <li><a href="properties.html">Properties</a></li>
            <li><a href="rollover.html">Log&nbsp;Rolling</a></li>
            <li><a href="log4jComparison.html">Comparison&nbsp;with&nbsp;Log4J</a></li>
            <li><a href="faq.html">FAQ</a></li>
            <li><a href="donations.html">Donations</a></li>
            <li><a href="meaningOfLife.html">The Meaning of Life</a></li>
            <li><a href="api/index.html">API Documentation</a></li>
         </ul>
      </div>

      <h1>Simple Log - Recommended Use</h1>

      <img src="images/lostStickman1.png" alt="Lost?" class="illustration"/>

      <p>
         Here's just a few tips about how to use Simple Log in your applications.<br/>
         If you haven't already read the <a href="quickStart.html">Quick Start</a>, you should go read it first, as it contains a number of important steps to follow when using Simple Log that won't be repeated here.
      </p>


      <h2>
         When and Where Should I Log?
      </h2>

      <p>
         Obviously, this is one of the most important questions when using logging. In general, I try to think about this when deciding what to log:
      </p>
      <p class="emphasis">
         I can't add logging at runtime, I can only turn it on and off.
      </p>
      <p>
         If you can get this drilled into your head and start to make logging a natural part of writing code, you will find your logging will be much more useful once your application is being run. This is especially true once it goes into production and you don't have an opportunity to add more logging; if what you logged when you first wrote the code wasn't sufficient to debug your problem, it's too late.
      </p>

      <p>
         Let's talk practically. I usually log the entry into and exit out of every method that isn't a simple accessor or mutator. If there is a fork in my code (e.g. an <span class="code">if</span> or <span class="code">if/else</span>), I will either log straight after the fork (on both sides) or log the variable or variables that are used to determine how the fork is executed.
      </p>

      <p>
         Example:
<pre class="code">
if (record.isNew())
{
   log.debug("Creating ID for new record");
   ...
}</pre>
      </p>

      <p>
         Sometimes I'll <b>change the code</b> at forks like this <b>to accomodate my logging</b> requirements.
      </p>

      <p>
         Example:
<pre class="code">
if (file.exists() && file.isNormalFile() && file.canRead())
{
   ...
}
else
{
   ...
}

boolean exists = file.exists();
boolean normalFile = file.isNormalFile();
boolean readable = file.canRead();

log.verboseObject("exists", exists);
log.verboseObject("normalFile", normalFile);
log.verboseObject("readable", readable);

if (exists && normalFile && readable)
{
   ...
}
else
{
   ...
}</pre>
      </p>

      <p>
         Some people will have an immediate negative reaction to this kind of behaviour, usually int he form of "I'm not going to declare a VARIABLE just so I can log!" Fair enough. You don't have to. I find that this makes my code easier to read and my logging easier to write. But this page is called 'Recommended Use', so feel free to ignore anything which causes a violent reaction.
      </p>


      <h2>
         Exceptions
      </h2>

      <p>
         Whenever I catch an exception, I log it. This just makes sense. Exceptions indicate less-than-normal program flow, a condition you'll usually want to be appearing in the log pretty prominently.
      </p>

      <p>
         However, I have got into the habit of logging the a message about the exception at an appropriate level, but not always logging the actual exception at the same level. This is because I believe the exception details and stack trace are, in some cases, not 100% necessary.
      </p>

      <p>
         In general, if an exception is in some respects normal (e.g. FileNotFoundException) and the program can recover from it, I'll log a message at the Warn level and the actual exception at the Debug level:
<pre class="code">
log.warn("File not found exception");
log.dbe(DebugLevel.L5_DEBUG, e);</pre>
      </p>

      <p>
         If the exception is more unexpected than this kind, I'll usually log the exception and the message at the Error level as there's much less chance that the exception will be reproducible.
      </p>

      <p>
         One rule that I think is really important is never to catch an exception just for the purpose of logging it. So <b>don't do this</b>:
<pre class="code">
try
{
   ...
}
catch (MyException me)
{
   log.debugException(me);
   throw me;
}</pre>
      </p>

      <p>
         I have seen lots of code like this, and what often happens is that, when an exception does occur, it gets printed in the log between two and five times. A similar guideline applies if an exception is being caught just to be "wrapped" in another exception and "re-thrown". You can pretty safely assume that some other code will log the wrapping exception (if it needs to), including the information from the wrapped exception, and so it's really only necessary to log the wrapped exception at something like the Verbose level.
      </p>

      <p>
         As with all rules, there is an exception to this one (no pun intended), which is that you should catch exceptions just for the purpose of logging them at the boundaries of inter-process calls. That is, I surround the whole body of any implementation of a CORBA, Remote or Web Service interface with a try/catch like the one above, making sure to catch and throw every specific type of exception.
      </p>


      <h2>
         What to log at each level
      </h2>

      <img src="images/lostStickman2.png" alt="Lost?" class="illustration"/>

      <p>
         I think that what I believe should be logged at each level is pretty thoroughly documented in the javadoc for <a href="javadoc/org/grlea/log/DebugLevel.html#L1_FATAL">DebugLevel</a> <!-- (http://www.grlea.org/javadoc/simple-log/) -->.
      </p>

      <p>
         But one thing I'd like to add to this is this: avoid the temptation to log messages and objects in "less important" classes at the more verbose levels. The decision as to which class has log output that is more important will be made by the person looking at the log file. If you use the levels consistently throughout every class, it will make it much easier for that person to decide which level to set for each package or class.
      </p>


      <h2>
         When to use instance logging
      </h2>

      <p>
         Instance logging is when you create a SimpleLogger for each instance of a class, rather than just one for all instances of the class. The added value is that each message output by instance loggers contains an ID, provided by you, that identifies the instance which is logging the message. This makes it possible to determine not just in which class your code is executing, but also on which object.
      </p>

      <p>
         The questions to ask before thinking about using instance logging are:

         <ul>
            <li>Will multiple instances of this object be logging at the same time?</li>
            <li>Will it be important to be able to distinguish between the instances?</li>
            <li>Will two or more instances of your class be logging from the same thread, or will one instance execute code on multiple threads?</li>
         </ul>
      </p>

      <p>
         If the answer to <i>any</i> of these is 'no', you probably don't need instance logging, either because you'll be able to distinguish which instance is logging using some other information (e.g. when or in which thread the logging occurred) or because you just won't care which instance is logging. Of course, if you think there's a chance it might be useful info, it's sometimes better to be safe than sorry - you can't magically switch to instance logging at runtime.
      </p>

      <p>
         Just in case you're worried about creating a logger for each instance of a class, rest assured that Simple Log keeps a list of instance loggers as WeakReferences, meaning that as soon as the object using the logger is garbage collected, the instance logger should be as well.
      </p>


      <h2>
         How to log objects
      </h2>

      <p>
         Logging objects is really easy and really useful. It's demonstrated in the Quick Start, but just in case you missed it, you can do it like this:

<pre class="code">
Object someObject = ...;
int aCountOfSomething = ...;
boolean isPixelBlack = ...;

log.debugObject("someObject", someObject);
log.verboseObject("aCountOfSomething", aCountOfSomething);
log.ludicrousObject("isPixelBlack", isPixelBlack);</pre>
      </p>

      <p>
         Or, if you need to log something other than an Object, int or boolean, or at a level other than Debug, Verbose or Ludicrous, you can use the general-purpose <span class="code">dbo()</span> methods:

<pre class="code">
long objectId = ...;
log.dbo(DebugLevel.L4_INFO, "objectId", objectId);</pre>
      </p>

      <p>
         Note that you should avoid doing this (seeing as you don't need to):
<pre class="code">
log.debug("objectId: " + objectId);</pre>

         and there's no need to do this,

<pre class="code">
if (log.wouldLog(DebugLevel.L5_DEBUG))
   log.debugObject("objectId", objectId);</pre>

         because Simple Log does the level check internally before string-ifying the value.
      </p>


      <h2>
         When to use wouldLog()
      </h2>

      <p>
         So, when <i>would</i> you use the <span class="code">wouldLog()</span> method?
      </p>

      <p>
         The rule for when to use <span class="code">wouldLog()</span> is exactly the same as that for using Log4J's <span class="code">isDebugEnabled()</span> method, it's just that Simple Log's <span class="code">dbo()</span>-derived methods relegate most of the use cases where this has traditionally been necessary.
      </p>

      <p>
         The rule is: If work has to be done to produce the value that will be passed into Simple Log, you should test with <span class="code">wouldLog()</span> first.
      </p>

      <p>
         So, for example, if you are planning on transforming an XML Document into a string and logging that, you'd want to check that it will be logged before you do the transformation. However, if you have a representation of that document which has a <span class="code">toString()</span> method that will produce the output you want, there's no need to do a check the level before logging, even if the <span class="code">toString()</span> method does a lot of work, because Simple Log will check the level internally before invoking the <span class="code">toString()</span> method.
      </p>


      <h2>
         Log Bridge
      </h2>

      <p>
         If you're writing a software framework or a component which you intend to be used in many applications then some people who you intend to use your software might consider it rude if you force them to use Simple Log by using it in your software.
      </p>

      <p>
         The solution to this problem is to use a bridge, a library that presents a simple interface for logging that can be implemented using many different logging packages. I have written such a library, and called it <a href="https://log-bridge.dev.java.net/">Log Bridge</a>. It was written specifically with the purpose of supporting the majority of Simple Log's features, but also comes with implementations that bridge to Log4J, the JDK Logging introduced in J2SE 1.4 (java.util.logging) and a couple of other logging packages.
      </p>

      <p class="copyright">
         Copyright (c) 2006 Graham Lea. All rights reserved.
      </p>

      <div class="bottomMenu">
         <h1>Simple Log User Guide</h1>
         <ul>
            <li><a href="quickStart.html">Quick&nbsp;Start</a></li>
            <li><a href="philosophy.html">Philosophy</a></li>
            <li><a href="recommendedUse.html">Recommended&nbsp;Use</a></li>
            <li><a href="properties.html">Properties</a></li>
            <li><a href="rollover.html">Log&nbsp;Rolling</a></li>
            <li><a href="log4jComparison.html">Comparison&nbsp;with&nbsp;Log4J</a></li>
            <li><a href="faq.html">FAQ</a></li>
            <li><a href="donations.html">Donations</a></li>
            <li><a href="meaningOfLife.html">The Meaning of Life</a></li>
            <li><a href="api/index.html">API Documentation</a></li>
         </ul>
      </div>

   </body>

</html>
